package com.mw.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mw.commons.utils.StringUtils;
import com.mw.mapper.SysLoginUserMapper;
import com.mw.pojo.SysLoginUser;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName: UserServiceImpl
 * 类文件简单描述:
 * @Author: MW
 * @CreateDate: 2022/2/11 16:01
 */
@Service
public class UserServiceImpl extends ServiceImpl<SysLoginUserMapper, SysLoginUser> implements IUserService {

    @Override
    public List<SysLoginUser> getUserListCondition(String condition) throws Exception {
        String conditionStr = new String(Base64.decodeBase64(condition), "UTF-8");
        JSONObject jsonObject = JSONObject.parseObject(conditionStr);
        String userType = jsonObject.getString("userType");
        String status = jsonObject.getString("status");
        QueryWrapper<SysLoginUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(userType), "user_type", userType).eq(!StringUtils.isEmpty(status), "status", status);
        List<SysLoginUser> sysLoginUsers = baseMapper.selectList(queryWrapper);
        for (SysLoginUser user : sysLoginUsers) {
            if (!Objects.isNull(user.getGender())) {
                user.setGender(user.getGender().equals("0") ? "男" : "女");
            } else {
                user.setGender("未知");
            }
            user.setUserType(user.getUserType().equals("0") ? "管理员" : "普通用户");
            user.setStatus(user.getStatus().equals("0") ? "正常" : "停用");
        }
        return sysLoginUsers;
    }
}